package com.gdpa.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdpa.entity.SysUser;
import com.gdpa.entity.vo.ResponseData;
import com.gdpa.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author zsr
 * @since 2018-12-19
 */
@RestController
@RequestMapping("/sys-user")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @RequestMapping("/list")
    public ResponseData list(Page page,@RequestParam(value = "search", required = false) String search){
        List<SysUser> sysUserList = sysUserService.userPage(page,search);
        return ResponseData.okPage(page.getTotal(),sysUserList);
    }

    @PostMapping("/save-update")
    public ResponseData saveOrUpdate(SysUser sysUser){
        boolean flg = sysUserService.saveOrUpdate(sysUser);
        if (flg){
            return ResponseData.okMsg("操作成功！");
        }
        return ResponseData.failMsg("操作失败！");
    }

    @PostMapping("/reset/{id}")
    public ResponseData resetPwd(@PathVariable("id") Integer id){
        boolean flg = sysUserService.resetPwd(id);
        if (flg){
            return ResponseData.okMsg("密码重置成功！");
        }
        return ResponseData.failMsg("密码重置失败！");
    }

    @PostMapping("/del/{id}")
    public ResponseData delUser(@PathVariable("id") Integer id){
        boolean flg = sysUserService.removeById(id);
        if (flg){
            return ResponseData.okMsg("删除成功！");
        }
        return ResponseData.failMsg("删除失败！");
    }

    @RequestMapping("/getUser")
    public ResponseData getUser(Integer id){
        SysUser sysUser = sysUserService.showMessage(id);

        return ResponseData.okData(sysUser);
    }

    @PostMapping("/modify")
    public ResponseData modify(SysUser sysUser){
        String userID = sysUser.getUserId().toString();
        System.out.println(userID);
        boolean flg = sysUserService.modifyMessage(sysUser);
        if (flg){
            return ResponseData.okMsg("删除成功！");
        }
        return ResponseData.failMsg("删除失败！");
    }
}
